package com.careersersystem.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.careersersystem.entity.Employer;

/**
 * A data access object (DAO) providing persistence and search support for
 * Employer entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.careersersystem.entity.Employer
 * @author MyEclipse Persistence Tools
 */

public class EmployerDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(EmployerDAO.class);
	// property constants
	public static final String CNAME = "cname";
	public static final String COM_TYPE = "comType";
	public static final String LOCATION = "location";
	public static final String PHONE = "phone";
	public static final String EMAIL = "email";
	public static final String DESCRIPTION = "description";

	public void save(Employer transientInstance) {
		log.debug("saving Employer instance");
		try {
			Transaction tx=getSession().beginTransaction();
			getSession().save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Employer persistentInstance) {
		log.debug("deleting Employer instance");
		try {
			Transaction tx=getSession().beginTransaction();
			getSession().delete(persistentInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Employer findById(java.lang.Integer id) {
		log.debug("getting Employer instance with id: " + id);
		try {
			Employer instance = (Employer) getSession().get(
					"com.careersersystem.entity.Employer", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Employer instance) {
		log.debug("finding Employer instance by example");
		try {
			List results = getSession().createCriteria(
					"com.careersersystem.entity.Employer").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Employer instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Employer as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCname(Object cname) {
		return findByProperty(CNAME, cname);
	}

	public List findByComType(Object comType) {
		return findByProperty(COM_TYPE, comType);
	}

	public List findByLocation(Object location) {
		return findByProperty(LOCATION, location);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findAll() {
		log.debug("finding all Employer instances");
		try {
			String queryString = "from Employer";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Employer merge(Employer detachedInstance) {
		log.debug("merging Employer instance");
		try {
			Employer result = (Employer) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Employer instance) {
		log.debug("attaching dirty Employer instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Employer instance) {
		log.debug("attaching clean Employer instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}